from pygo1963.model.Board import Board

from pygo1963.model.Game import Game
from pygo1963.model import PlayersFactory
from pygo1963.view.gui import Button, ContenedorMovil, Label,\
    ListBox
from pygo1963.view.View import GameView
from pygo1963.view import gui

class Menu:

    def __init__(self, view):
        
        self.view = view
        
        self.current_game = None
        
        labelStyleCopy = view.gui.defaultLabelStyle.copy()
        labelStyleCopy['border-width'] = 1
        labelStyleCopy['wordwrap'] = True
        labelStyleCopy['autosize'] = False
        
        self.new_game_button = Button(position=(10, 50), size=(200, 0), 
                                    parent=self.view.desktop, text="Nueva Partida")
        self.new_game_button.count = 0
        
        self.new_game_button.onClick = self.new_game
        
    
    def new_game(self, new_game_button):
        
        self.config_window = ContenedorMovil(position=(230, 50), size=(350, 250), 
                                             parent=self.view.desktop, text="Create new Game")
        Label(text="Black:", parent=self.config_window, position=(50, 50))
        items = ["Human", "Pygo", "Random"]
        items2 = ["Pygo", "Human", "Random"]
        
        p1 = ListBox(items=items, parent=self.config_window, position=(115, 50))
            
        Label(text="White:", parent=self.config_window, position=(50, 100))
        p2 = ListBox(items=items2, parent=self.config_window, position=(115, 100))
            
        Label(text="Board Size:", parent=self.config_window, position=(50, 150))
        sizes = [str(size) for size in range(6, 17)]
        boardSize = ListBox(items=sizes, parent=self.config_window, position=(115, 150))
            
        play = Button(text="Start", parent=self.config_window, position=(50, 200))
        play.p1 = p1
        play.p2 = p2
        play.boardSize = boardSize
        
        play.onClick = self.create_new_game
        
    
    def create_new_game(self, button):
            
        button.parent.close()
        
        player1 = button.p1.items[button.p1.selectedIndex]
        player2 = button.p2.items[button.p2.selectedIndex]        
        players = PlayersFactory.create_players(player1, player2, self.view.controller)
                
        boardSize = int(button.boardSize.items[button.boardSize.selectedIndex])    
        game = Game(players[0], players[1], Board(boardSize))  
        
        GameView(self.view, game)  
     

    